<template>
  <div class="category_container">

    <el-row style="bottom:5px ">
      <el-col :span="6">
        <el-button size="small" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()" class="add_button">
          新增分类
        </el-button>
      </el-col>
      <el-col :span="18"></el-col>
    </el-row>


    <el-table
        :data="dataList"
        v-loading="dataListLoading"
        stripe
        style="width: 100%;">
      <el-table-column
          prop="id"
          header-align="center"
          align="center"
          width="50"
          label="ID">
      </el-table-column>
      <el-table-column
          prop="categoryName"
          header-align="center"
          align="center"
          label="分类名称"
      >
      </el-table-column>
      <el-table-column
          prop="color"
          header-align="center"
          align="center"
          label="标签颜色">
        <template v-slot="scope">
          <span style="width: 100px;height: 30px; display: inline-block"
                :style="`background-color:${scope.row.color}`"/>
        </template>
      </el-table-column>
      <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id,scope.row.categoryName)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>3
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>


<style lang="less" scoped>
.el-row {
  display: flex;
  justify-content: space-between;

  .add_button {
    float: left;
  }
}


</style>


<script>
import AddOrUpdate from './category-add-or-update'

export default {
  data() {
    return {
      dataList: [], //表格数据列表
      pageIndex: 1, //当前页
      pageSize: 10, //每页显示数据量
      totalCount: 0, //总页数
      dataListLoading: false, //加载
      addOrUpdateVisible: false //修改、增加对话框
    }
  },
  components: {
    AddOrUpdate
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: '/category/page',
        method: 'get',
        params: {
          'page': this.pageIndex,
          'limit': this.pageSize,
        }
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.data.list
          this.totalCount = data.data.totalCount
        } else {
          this.dataList = []
          this.totalCount = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      //dom渲染完成后再执行
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id, categoryName) {
      this.$confirm(`确定删除分类【${categoryName}】?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: `/category/delete/${id}`,
          method: 'post',
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>
